package net.trade.trade.action;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.core.base.struts.BaseAction;
import net.core.common.Constants;
import net.core.common.HttpUtil;
import net.core.common.StrUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.system.base.bean.SSysnotice;
import net.system.sys.service.INoticeService;
import net.trade.base.bean.MiyinMarket;
import net.trade.trade.service.ITradeService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.opensymphony.xwork2.ActionContext;

@Scope("request")
@Namespace("/trade")
@InterceptorRefs(@InterceptorRef("basicStack"))
@ParentPackage("struts-default")
@Results({ @Result(name = "error", location = "/error.jsp") })
public class TradeAction extends BaseAction {
	final Logger logger = LoggerFactory.getLogger(TradeAction.class);
	private static final long serialVersionUID = 1L;
	@Autowired
	private ITradeService service;

	@Autowired
	private INoticeService noticeService;

	private List<HashMap> listMsg; // 公告信息

	private List<HashMap> listHelp; // 帮助

	private SSysnotice notice;
	
	private HashMap<String,String> hmRetrun;

	// private List<HashMap> listContent;// 信息内容

	public HashMap<String, String> getHmRetrun() {
		return hmRetrun;
	}

	public void setHmRetrun(HashMap<String, String> hmRetrun) {
		this.hmRetrun = hmRetrun;
	}

	public List<HashMap> getListMsg() {
		return listMsg;
	}

	// public List<HashMap> getListContent() {
	// return listContent;
	// }
	//
	// public void setListContent(List<HashMap> listContent) {
	// this.listContent = listContent;
	// }

	public void setListMsg(List<HashMap> listMsg) {
		this.listMsg = listMsg;
	}

	public SSysnotice getNotice() {
		return notice;
	}

	public void setNotice(SSysnotice notice) {
		this.notice = notice;
	}

	public List<HashMap> getListHelp() {
		return listHelp;
	}

	public void setListHelp(List<HashMap> listHelp) {
		this.listHelp = listHelp;
	}

	/**
	 * 系统首页
	 * 
	 * @return
	 */
	@Action(value = "index1", results = { @Result(name = "success", location = "/WEB-INF/pages/trade/index.jsp") })
	public String index() {
		//
		HttpServletRequest request = getRequest();
		// HttpSession session = getSession();

		HashMap reqMap = new HashMap();
		reqMap = HttpUtil.buildParameterMap(getRequest());
		// MiyinMarket market = service.getMarket();
		request.setAttribute("market", "mark34678980-Set");
		// 获取公告信息
		listMsg = noticeService.selectTop(8,"notice");
		listHelp = noticeService.selectTop(8,"help");
		// request.setAttribute("listMsg", listMsg);

		return SUCCESS;
	}

	/**
	 * 首页内容
	 * 
	 * @return
	 */
	@Action(value = "content", results = { @Result(name = "success", location = "/WEB-INF/pages/trade/content.jsp") })
	public String content() {
		//
		HttpServletRequest request = getRequest();
		// HttpSession session = getSession();

		HashMap reqMap = new HashMap();
		reqMap = HttpUtil.buildParameterMap(getRequest());
		// 获取具体信息
		listMsg = noticeService.selectTop(8,"notice");
		listHelp = noticeService.selectTop(8,"help");
		notice = noticeService.getBean(StrUtils.getNotNullStrValue(reqMap
				.get("recId")));

		return SUCCESS;
	}

	/**
	 * 交易首页
	 * 
	 * @return
	 */
	@Action(value = "mainPage", results = { @Result(name = "success", location = "/WEB-INF/pages/trade/trade/TradeMain.jsp") })
	public String mainPage() {
		//
		HttpServletRequest request = getRequest();
		HttpSession session = getSession();

		// 加载行情数据
		HashMap reqMap = new HashMap();
		reqMap = HttpUtil.buildParameterMap(getRequest());
		// MiyinMarket market = service.getMarket();
		// request.setAttribute("market", market);

		return SUCCESS;
	}

	

	/**
	 * 加载交易行情
	 * 
	 * @return
	 */
	@Action(value = "trademy")
	public String trademy() {
		//
		HttpServletRequest request = getRequest();
		HttpSession session = getSession();

		// 加载行情数据
		HashMap reqMap = new HashMap();
		reqMap = HttpUtil.buildParameterMap(getRequest());
		service.tradeMY(reqMap);
		HttpServletResponse response = getResponse();
		PrintWriter out = null;
		try {
			response.setCharacterEncoding(Constants.SYS_ENCODE);
			out = response.getWriter();
			out.print("1");
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}

		return SUCCESS;
	}

	/**
	 * 获取市场行情信息
	 */
	@Action(value = "getMarket")
	public void getMarket() {
		HttpServletResponse response = getResponse();
		PrintWriter out = null;
		try {
			response.setCharacterEncoding(Constants.SYS_ENCODE);
			out = response.getWriter();
			MiyinMarket market = service.getMarket();
			out.print(JSONObject.fromObject(market).toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 获取市场行情信息
	 */
	@Action(value = "getMiyin")
	public void getMiyin() {
		HttpServletResponse response = getResponse();
		PrintWriter out = null;
		try {
			response.setCharacterEncoding(Constants.SYS_ENCODE);
			out = response.getWriter();
			String my = service.getMiyin();
			JSONObject json = new JSONObject();
			json.put("miyin", my);
			out.print(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 获取用户持仓信息
	 */
	@Action(value = "getUserOrder")
	public void getUserOrder() {
		HttpServletRequest request = getRequest();
		String userId = request.getParameter("userId");
		HttpServletResponse response = getResponse();
		PrintWriter out = null;
		try {
			response.setCharacterEncoding(Constants.SYS_ENCODE);
			out = response.getWriter();
			List<HashMap> market = service.getUserOrder(userId);
			// TODO 转换出错,原因出在日期身上
			System.out.println(JSONArray.fromObject(market).toString());
			out.print(JSONArray.fromObject(market).toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 获取用户账号信息
	 */
	@Action(value = "getUserAccount")
	public void getUserAccount() {
		HttpServletRequest request = getRequest();
		String userId = request.getParameter("userId");
		HttpServletResponse response = getResponse();
		PrintWriter out = null;
		try {
			response.setCharacterEncoding(Constants.SYS_ENCODE);
			out = response.getWriter();
			List<HashMap> market = service.getUserAccount(userId);
			HashMap map = null;
			if (null != market && market.size() > 0) {
				map = market.get(0);
			}
			System.out.println(JSONObject.fromObject(map).toString());
			out.print(JSONObject.fromObject(map).toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
}
